/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaclass;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Vincent
 */
public class RandomString {
    private String validCharacters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    
    private String shuffle(String input){
        List<Character> characters = new ArrayList<Character>();
        for(char c:input.toCharArray()){
            characters.add(c);
        }
        StringBuilder output = new StringBuilder(input.length());
        while(!characters.isEmpty()){
            int randPicker = (int)(Math.random()*characters.size());
            output.append(characters.remove(randPicker));
        }
        return output.toString();
    }
    
    public String RandomStringWithLength(int length){
        String randomString = "";
        do{
            randomString += shuffle(validCharacters);
        }
        while (randomString.length() <= length);
        int randomBeginIndex = (int)(Math.random()*(randomString.length()-length));
        randomString = randomString.substring(randomBeginIndex, randomBeginIndex + length);
        return randomString;
    }
}
